package com.aaa.tars.controller;

import com.aaa.tars.entity.Menu;
import com.aaa.tars.entity.Role;
import com.aaa.tars.service.MenuService;
import com.aaa.tars.service.RoleService;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @FileName: RoleController
 * @Description:
 * @Author: xm
 * @CreateTime: 2025/9/9 20:33
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("role")
@Log4j2
public class RoleController {

    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;


    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Role> page) {
        return ResultUtil.success(this.roleService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param roleId 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(Integer roleId) {
        return ResultUtil.success(this.roleService.queryById(roleId));
    }

    /**
     * 新增数据
     *
     * @param role 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Role role) {
        return ResultUtil.success(this.roleService.insert(role));
    }

    /**
     * 编辑数据
     *
     * @param role 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Role role) {
        return ResultUtil.success(this.roleService.update(role));
    }

    /**
     * 删除数据
     *
     * @param roleId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer roleId) {
        return ResultUtil.success(this.roleService.deleteById(roleId));
    }

    /**
     * 查询所有角色
     *
     * @return 角色列表
     */
    @GetMapping("queryAll")
    public Result queryAll() {
        return ResultUtil.success(this.roleService.queryAll());
    }

    /**
     * 根据角色名查询
     *
     * @param roleName 角色名
     * @return 角色列表
     */
    @GetMapping("queryByRoleName")
    public Result queryByRoleName(String roleName) {
        return ResultUtil.success(this.roleService.queryByRoleName(roleName));
    }

    //获取角色权限
    @GetMapping("/menus/{roleId}")
    public Result<List<Menu>> getRoleMenus(@PathVariable Integer roleId) {
        List<Menu> menus = roleService.getMenusByRoleId(roleId);
        return ResultUtil.success(menus);
    }

    @PostMapping("/assignMenus")
    public Result<String> assignMenus(@RequestBody Map<String, Object> params) {
        Integer roleId = (Integer) params.get("roleId");
        List<Integer> menuIds = (List<Integer>) params.get("menuIds");
        System.out.println("=============================================");
        System.out.println("controller授权Id:=============="+menuIds);
        System.out.println("=============================================");
        roleService.assignMenus(roleId, menuIds);
        return ResultUtil.success("分配权限成功");
    }

    @GetMapping("/menus/all")
    public Result getAllMenus() {
        return ResultUtil.success(menuService.queryTreeData());
    }
}
